﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using System.Windows.Navigation;

namespace FasterWPF
{
    public static class MenuExt
    {
        //Due to order of precedence issues, only set Background Color via styles 
        //public static Menu SetBackgroundColor(this Menu chain, Brush brush)
        //{
        //    Brush newBrush = (brush);
        //    chain.Background = newBrush;
        //    return chain;
        //}

        public static void ChangeBackgroundColorOnMenu(Menu mnu, Brush newColor)
        {
            if (mnu != null)
            {
                Style style = new Style(typeof(Menu), mnu.Style);

                Setter setter = new Setter();
                setter.Property = Menu.BackgroundProperty;
                setter.Value = newColor;
                style.Setters.Add(setter);

                mnu.Style = style;
            }
        }

        public static Menu SetMouseOverColorOnMenu(this Menu mnu, Brush brush)
        {
            Style style = new Style(typeof(Menu), mnu.Style);
            Trigger t = new Trigger();
            t.Property = Menu.IsMouseOverProperty;
            t.Value = true;
            Setter setter = new Setter();
            setter.Property = Menu.BackgroundProperty;
            setter.Value = brush;
            t.Setters.Add(setter);

            style.Triggers.Add(t); // Important: add a trigger before applying style
            mnu.Style = style;

            return mnu;
        }
                            
        public static Menu Initialize(this Menu source, double width, double height, ContainerType containerType, Brush background, Brush foreground, string family, double fontSize, FontWeight weights, FontStyle fontStyle)
        {
            //Force the Aero theme by default to assure the structure of the Menu
            Uri uri = new Uri("/PresentationFramework.Aero;component/themes/Aero.NormalColor.xaml", UriKind.Relative);
            source.Resources.MergedDictionaries.Add(Application.LoadComponent(uri) as ResourceDictionary);

            source.IsMainMenu = true;

            ChangeBackgroundColorOnMenu(source, background);

            sharedMenuInitializeCode(source, width, height, containerType);

            return source;
        }

        public static Menu Initialize(this Menu source, double width, double height, ContainerType containerType, Brush background)
        {
            //Force the Aero theme by default to assure the structure of the Menu
            Uri uri = new Uri("/PresentationFramework.Aero;component/themes/Aero.NormalColor.xaml", UriKind.Relative);
            source.Resources.MergedDictionaries.Add(Application.LoadComponent(uri) as ResourceDictionary);

            source.IsMainMenu = true;

            ChangeBackgroundColorOnMenu(source, background);

            sharedMenuInitializeCode(source, width, height, containerType);

            return source;
        }

        public static Menu Initialize(this Menu source, double width, double height, ContainerType containerType)
        {
            //Force the Aero theme by default to assure the structure of the Menu
            Uri uri = new Uri("/PresentationFramework.Aero;component/themes/Aero.NormalColor.xaml", UriKind.Relative);
            source.Resources.MergedDictionaries.Add(Application.LoadComponent(uri) as ResourceDictionary);

            source.IsMainMenu = true;

            sharedMenuInitializeCode(source, width, height, containerType);

            return source;
        }

        private static void sharedMenuInitializeCode(Menu source, double width, double height, ContainerType containerType)
        {
            source.HorizontalAlignment = HorizontalAlignment.Left;
            source.VerticalAlignment = VerticalAlignment.Top;

            source.Width = width;
            source.Height = height;
            source.SetContainerType(containerType);
            IComposite icomp = CompositeTypeHelper.CreateIComposite<Menu>(source);
            source.SetIComposite<Menu>(icomp);

            //STORE SETTING BUILD CHAIN AND GET CHAIN?!?  Put these in a Composite implements an interface
            MenuParent gbsr = new MenuParent();
            source.StoreSetting<Menu>("IParent", gbsr);

            source.StoreSetting<Menu>("ObsCol", new ObservableCollection<MenuItem>());

            source.AddItemBorder(Brushes.Gray, new Thickness(1, 1, 1, 1)); //default Border     
            source.StoreSetting<Menu>("BorderSettings", new BorderSettings() { Width = double.NaN, Height = double.NaN, BorderBrush = Brushes.Gray, BorderThickness = new Thickness(0, 0, 0, 0), CornerRadius = new CornerRadius(0), Padding = new Thickness(0) });
        }
                
    }//end of class
}//end of namespace
